package iammry.com.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

public class ChartService {

    private GraphicalView mGraphicalView;
    private XYMultipleSeriesDataset multipleSeriesDataset;// 数据集容器
    private XYMultipleSeriesRenderer multipleSeriesRenderer;// 渲染器容器
    private XYSeries mSeries;// 单条曲线数据集
    private XYSeriesRenderer mRenderer;// 单条曲线渲染器
    private Context context;

    public ChartService(Context context) {
        this.context = context;

    }

    /**
     * 获取图表
     *
     * @return
     */
    public GraphicalView getGraphicalView() {

        mGraphicalView = ChartFactory.getCubeLineChartView(context,
                multipleSeriesDataset, multipleSeriesRenderer, 0.4f);

        return mGraphicalView;
    }

    /**
     * 获取数据集，及xy坐标的集合
     *
     * @param //curveTitle
     */
    public void setXYMultipleSeriesDataset() {

        multipleSeriesDataset = new XYMultipleSeriesDataset();
        mSeries = new XYSeries("");
        multipleSeriesDataset.addSeries(mSeries);
    }


    /**
     * 获取渲染器
     *
     * @param maxX       x轴最大值
     * @param maxY       y轴最大值
     * @param axeColor   坐标轴颜色
     * @param labelColor 标题颜色
     * @param curveColor 曲线颜色
     * @param gridColor  网格颜色
     */
    public void setXYMultipleSeriesRenderer(double maxX, double maxY,
                                            int axeColor,
                                            int labelColor, int curveColor, int gridColor) {
        multipleSeriesRenderer = new XYMultipleSeriesRenderer();

        multipleSeriesRenderer.setRange(new double[]{0, maxX, 0, maxY});//xy轴的范围
        multipleSeriesRenderer.setLabelsColor(labelColor);
        //设置等分
        multipleSeriesRenderer.setYLabels(5);
        multipleSeriesRenderer.setXLabels(7);

        //设置坐标值的位置
        multipleSeriesRenderer.setXLabelsAlign(Align.CENTER);
        multipleSeriesRenderer.setYLabelsAlign(Align.CENTER);
        multipleSeriesRenderer.setYLabelsVerticalPadding(5);
        //  multipleSeriesRenderer.setAxisTitleTextSize(20);
        // multipleSeriesRenderer.setChartTitleTextSize(20);

        multipleSeriesRenderer.setLabelsTextSize(20);
        multipleSeriesRenderer.setLegendTextSize(35);

        //   multipleSeriesRenderer.setPointSize(1f);//曲线描点尺寸

        multipleSeriesRenderer.setFitLegend(false);
        //上 左 下 右
        multipleSeriesRenderer.setMargins(new int[]{30, 40, 0, 50});
        //限制表的移动范围
        multipleSeriesRenderer.setPanLimits(new double[]{0, 0, 0, 0});
        //设置只显示x轴
        multipleSeriesRenderer.setShowGridX(true);
        //设置能不能放大
        multipleSeriesRenderer.setZoomEnabled(false, false);
        //设置曲线的颜色
        multipleSeriesRenderer.setAxesColor(axeColor);
        //设置网格线的颜色
        multipleSeriesRenderer.setGridColor(gridColor);

        //x轴不显示数字
        multipleSeriesRenderer.setXLabels(0);


        multipleSeriesRenderer.setShowLegend(true);
        //   multipleSeriesRenderer.setXLabelsPadding(5);


        List<String> datas = getSevendate();
        for (int i = 0; i < datas.size(); i++)
        {
            multipleSeriesRenderer.addTextLabel(i, datas.get(i));
        }

        multipleSeriesRenderer.setBackgroundColor(Color.WHITE);//背景色
        multipleSeriesRenderer.setMarginsColor(Color.WHITE);//边距背景色，默认背景色为黑色，这里修改为白色
        mRenderer = new XYSeriesRenderer();
        mRenderer.setColor(curveColor);
        mRenderer.setLineWidth(5f);


        //mRenderer.setPointStyle(PointStyle.CIRCLE);//描点风格，可以为圆点，方形点等等
        multipleSeriesRenderer.addSeriesRenderer(mRenderer);
    }

    /**
     * 根据新加的数据，更新曲线，只能运行在主线程
     *
     * @param x 新加点的x坐标
     * @param y 新加点的y坐标
     */
    public void updateChart(double x, double y) {
        mSeries.add(x, y);
        mGraphicalView.repaint();//此处也可以调用invalidate()
    }

    /**
     * 添加新的数据，多组，更新曲线，只能运行在主线程
     *
     * @param xList
     * @param yList
     */
    public void updateChart(List<Double[]> xList, List<Double[]> yList) {


        for (int i = 0; i <7; i++) {
           /// mSeries.add(xList.get(i), yList.get(i));
            Double[] xV = xList.get(i);
            Double[] yV = yList.get(i);

            for (int k = 0; k < 7; k++) {
                mSeries.add(xV[k], yV[k]);
            }
            multipleSeriesDataset.addSeries(mSeries);
        }
        mGraphicalView.repaint();//此处也可以调用invalidate()
    }


    /**
     * 获取今天往后一周的日期（几月几号）
     */
    public static List<String> getSevendate() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));


        for (int i = 0; i < 7; i++) {
            String mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) - i);// 获取当前日份的日期号码
            if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
                mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
            }
            String date = mMonth + "." + mDay;
            dates.add(date);
        }
        return dates;
    }

    /**
     * 得到当年当月的最大日期
     **/
    public static int MaxDayFromDay_OF_MONTH(int year, int month) {
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);//注意,Calendar对象默认一月为0
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }

}